﻿
/*
Author: liyonghelpme
Email: 233242872@qq.com
*/

/*
Author: liyonghelpme
Email: 233242872@qq.com
*/
using UnityEngine;
using System.Collections;

using System;
using KBEngine;

namespace ChuMeng
{
	public class PlayerSync : KBEngine.MonoBehaviour
	{
		/*
		 * Write Message Send To Server
		 * PlayerManagerment  PhotonView Manager 
		 */ 
		int lastGridX = -1;
		int lastGridZ = -1;
		public void OnPhotonSerializeView (Packet packet)
		{
			if (photonView.IsMine) {
				if(AstarPath.active != null && AstarPath.active.graphs.Length > 0) {
					CGPlayerMove.Builder mv = CGPlayerMove.CreateBuilder ();
					var vxz = Util.CoordToGrid(transform.position.x, transform.position.z);
					//MapY Offset Height
					mv.X = Convert.ToInt32( vxz.x);
					mv.Y = Util.IntYOffset(transform.position.y);
					mv.Z = Convert.ToInt32(vxz.y);
					if(mv.X == lastGridX && mv.Z == lastGridZ) {
						return;
					}
					if(Util.CheckMovable((int)mv.X, (int)mv.Z)) {
						lastGridX = mv.X;
						lastGridZ = mv.Z;
						packet.protoBody = mv.BuildPartial ();
						return;
					}else {
						Log.Sys("Can't Move Grid "+mv.X+" "+mv.Z+" "+gameObject.name);
					}
				}

				packet.protoBody = null;

			} else {
				Debug.Log("Not Mine Push Move Command");
				var ms = packet.protoBody as MotionSprite;
				var vxz = Util.GridToCoord(ms.X, ms.Z);

				var mvTarget = new Vector3(vxz.x, 0, vxz.y);

				
			}

		}

	
	}

}